package sketchpad.business.databuffer.bufferpool.impl;

import sketchpad.business.databuffer.bufferpool.BufferPoolService;
import sketchpad.business.datacalc.calchelper.CalcHelperService;
import sketchpad.business.datacalc.calchelper.impl.CalcHelperServiceImpl;
import sketchpad.constant.Config;
import sketchpad.constant.Geometry;
import sketchpad.gui.control.ElementFactory;
import sketchpad.gui.drawinggraph.*;
import sketchpad.gui.drawinggraph.Random;
import sketchpad.gui.control.DrawControl;

import java.util.*;

//这里保存需要剪切复制粘贴的中间的图形
public final class BufferPoolServiceImpl implements BufferPoolService {

    private List<DrawItem> bufferItemList;
    private List<Group> bufferGroupList;
    private DrawControl control;

    public BufferPoolServiceImpl(DrawControl drawControl){
        control = drawControl;
        bufferItemList = new ArrayList<>();
        bufferGroupList = new ArrayList<>();
    }

    /*
    * 向BufferPool增加选中列表对象
    * 需要增加所在组的所有图形，并且修改drawControl的selectedItemList
    * @param : isCut 如果剪切的时候需要把在drawControl中的图形删除
    * */
    @Override
    public void add(){
        List<Integer> selectedItemList = control.getSelectedItemList();
        Map<Integer, DrawItem> itemMap = control.getDrawItemMap();
        Map<Integer, Group> groupMap = control.getGroupMap();
        for (int i : selectedItemList){
            if (i== Config.SELECT_ID)continue;
            DrawItem item = clone(itemMap.get(i));
            Group group = groupMap.get(item.getGroupId());
            if (group==null){
                if (!bufferItemList.contains(item)){
                    bufferItemList.add(item);
                }
            }else{
                for (DrawItem memberItem : group.getItemList()){//将成员增加到BufferItemMap
                    if (!bufferItemList.contains(memberItem)){
                        bufferItemList.add(item);
                    }
                }
                bufferGroupList.add(group);
            }
        }
    }

    //从鼠标所在位置开始粘贴bufferPool的图形
    //先找一个图形，记录其原位置，然后根据鼠标位置修改其位置，之后其它的图形根据其原来的相对位置修改为正确的位置
    @Override
    public void restore(){
            for (DrawItem item : bufferItemList){
                item.setId(control.getGlobalId());
        }
        moveItemWithItemList(bufferItemList);

        for (DrawItem item : bufferItemList){
            control.getDrawItemMap().put(item.getId(),item);
        }
        for (Group group : bufferGroupList){
            control.getGroupMap().put(group.getId(),group);
        }
        bufferItemList.clear();
        bufferGroupList.clear();
    }

    //duplicate with CalcHelperServiceImpl.moveItem()
    private void moveItemWithItemList(List<DrawItem> itemList){
        CalcHelperService calcHelperService = new CalcHelperServiceImpl(control);
        for (int i = 0; i < itemList.size(); i++){
            DrawItem item = calcHelperService.modifyNewCor(itemList.get(i),10,10);
            itemList.set(i,item);
        }
    }

    private DrawItem clone(DrawItem item){
        DrawItem drawItem = ElementFactory.getInstance(control).create(item.getGeometryClass(),item.getLeftUpNode());
        drawItem.setRightDownNode(item.getRightDownNode());
        if (item.getGeometryClass()== Geometry.RANDOM)((Random)drawItem).setNodeList(((Random)item).getNodeList());
        drawItem.setGroupId(item.getGroupId());
        return drawItem;
    }

}